#include "classform.h"
#include "ui_classform.h"

#include <QFile>
#include <QTextStream>
#include <QDebug>

ClassForm::ClassForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ClassForm)
{
    ui->setupUi(this);

    m_currentRow = -1;

    readFile();
    initWidgetUnits();
    on_pb_search_clicked();
}

ClassForm::~ClassForm()
{
    saveFile();
    delete ui;
}

void ClassForm::readFile()
{
    qDebug() << "ClassForm::readFile()";
    QFile f("../EduMSForDF/datafiles/class.txt");
    if(f.open(QIODevice::ReadOnly))
    {
        ClassInfo info;
        QString id, name, room, count;
        QTextStream ts(&f);
        while(!ts.atEnd())
        {
            ts >> id >> name >> room >> count;
            if(!id.isEmpty())
            {
                info = ClassInfo(id, name, room, count);
                m_infoList.append(info);
            }
        }
        f.close();
    }
}

void ClassForm::saveFile()
{
    qDebug() << "ClassForm::saveFile()";
    QFile f("../EduMSForDF/datafiles/class.txt");
    if(f.open(QIODevice::WriteOnly | QIODevice::Truncate))
    {
        QTextStream ts(&f);
        for(int i = 0; i < m_infoList.length(); i++)
        {
            ts << m_infoList.at(i).getID() << "\t\t" << m_infoList.at(i).getName() << "\t\t"
                 << m_infoList.at(i).getRoom() << "\t\t" << m_infoList.at(i).getCount() <<'\n';
        }
        f.close();
    }
}

void ClassForm::updateTableInfos(void)
{
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setRowCount(m_infoPtrList.length());

    // set header lables
    QStringList headers;
    headers << "班级号" << "名称"  << "教室" << "人数";
    ui->tableWidget->setHorizontalHeaderLabels(headers);
    for(int i=0; i < m_infoPtrList.count(); i++)
    {
        QTableWidgetItem *item = new QTableWidgetItem(m_infoPtrList.at(i)->getID());
        ui->tableWidget->setItem(i, 0, item);
        item = new  QTableWidgetItem(m_infoPtrList.at(i)->getName());
        ui->tableWidget->setItem(i, 1, item);
        item = new  QTableWidgetItem(m_infoPtrList.at(i)->getRoom());
        ui->tableWidget->setItem(i, 2, item);
        item = new  QTableWidgetItem(m_infoPtrList.at(i)->getCount());
        ui->tableWidget->setItem(i, 3, item);
    }
}

void ClassForm::on_tableWidget_clicked(const QModelIndex &index)
{
    m_currentRow = index.row();
    m_operInfo = m_infoPtrList.at(m_currentRow);
    ui->le_id->setText(m_operInfo->getID());
    ui->le_name->setText(m_operInfo->getName());
    ui->le_room->setText(m_operInfo->getRoom());
    ui->le_count->setText(m_operInfo->getCount());
}

void ClassForm::on_pb_search_clicked()
{
    m_infoPtrList.clear();
    if(m_searchCond == Search_ID)
    {
        for(int i = 0; i < m_infoList.length(); i++)
        {
            if(ui->le_condition->text() == m_infoList.at(i).getID())
            {
                m_infoPtrList.append(&(m_infoList[i]));
            }
        }
    }else if(m_searchCond == Search_Name)
    {
        for(int i = 0; i < m_infoList.length(); i++)
        {
            if(ui->le_condition->text() == m_infoList.at(i).getName())
            {
                m_infoPtrList.append(&(m_infoList[i]));
            }
        }
    }else if(m_searchCond == Search_Room)
    {
        for(int i = 0; i < m_infoList.length(); i++)
        {
            if(ui->le_condition->text() == m_infoList.at(i).getRoom())
            {
                m_infoPtrList.append(&(m_infoList[i]));
            }
        }
    }else
    {
        for(int i = 0; i < m_infoList.length(); i++)
        {
            m_infoPtrList.append(&(m_infoList[i]));
        }
    }

    updateTableInfos();
}

void ClassForm::on_cb_condition_currentIndexChanged(int index)
{
    m_searchCond = index;
    if(m_searchCond == 0)
    {
        ui->le_condition->setEnabled(false);
    }else
    {
        ui->le_condition->setEnabled(true);
    }
}

void ClassForm::initWidgetUnits(void)
{
    ui->le_condition->setEnabled(false);
    ui->le_id->setEnabled(false);
    ui->le_name->setEnabled(false);
    ui->le_room->setEnabled(false);
    ui->le_count->setEnabled(false);
    ui->pb_save->setEnabled(false);
}

void ClassForm::on_pb_cancel_clicked()
{
    ui->le_id->clear();
    ui->le_name->clear();
    ui->le_room->clear();
    ui->le_count->clear();

    ui->le_id->setEnabled(false);
    ui->le_name->setEnabled(false);
    ui->le_room->setEnabled(false);
    ui->le_count->setEnabled(false);
    ui->pb_save->setEnabled(false);
}

void ClassForm::on_pb_modify_clicked()
{
    m_operData = Oper_Mdy;
    ui->le_name->setEnabled(true);
    ui->le_room->setEnabled(true);
    ui->le_count->setEnabled(true);
    ui->pb_save->setEnabled(true);
}

void ClassForm::on_pb_delete_clicked()
{
    m_operData = Oper_Del;
    ui->le_name->setEnabled(true);
    ui->le_room->setEnabled(true);
    ui->le_count->setEnabled(true);
    ui->pb_save->setEnabled(true);
}

void ClassForm::on_pb_add_clicked()
{
    m_operData = Oper_Add;
    ui->le_id->setEnabled(true);
    ui->le_name->setEnabled(true);
    ui->le_room->setEnabled(true);
    ui->le_count->setEnabled(true);
    ui->pb_save->setEnabled(true);
}

void ClassForm::on_pb_save_clicked()
{
    if(m_operData == Oper_Mdy)
    {
        m_operInfo->setID(ui->le_id->text());
        m_operInfo->setName(ui->le_name->text());
        m_operInfo->setRoom(ui->le_room->text());
        m_operInfo->setCount(ui->le_count->text());
    }else if(m_operData == Oper_Del)
    {
        m_infoList.removeOne(*m_operInfo);
    }else if(m_operData == Oper_Add)
    {
        ClassInfo info(ui->le_id->text(),
                         ui->le_name->text(),
                         ui->le_room->text(),
                         ui->le_count->text());
        m_infoList.append(info);
    }

    on_pb_cancel_clicked();
    on_pb_search_clicked();
}
